package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.WxUserDao;
import com.ktjy.ktmall.entity.WxUser;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @auth wzy
 * @date 2024/10/31 15:37
 */
public class WxUserDaoImpl extends BaseDao implements WxUserDao {
    @Override
    public WxUser findByMobile(String mobile) {
        return null;
    }

    @Override
    public WxUser findByOpenId(String openId) {
        WxUser user = null;
        String sql = "select * from `t_wx_user` where `open_id` = ?";
        rs = super.executeQuery(sql, openId);
        try {
            if (rs.next()) {
                user = new WxUser(rs.getInt("id"), rs.getString("username"),
                        rs.getInt("gender"), rs.getString("birthday"),
                        rs.getString("nickname"), rs.getString("mobile"),
                        rs.getString("avatar"), rs.getString("fullLocation"),
                        rs.getString("profession"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int insert(WxUser user) {
        String sql = "insert into `t_wx_user`(username, password, `last_login_time`, `last_login_ip`, nickname, mobile, avatar,open_id) values(?,?,?,?,?,?,?,?)";
        return super.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getLastLoginTime(), user.getLastLoginIp(), user.getNickname(), user.getMobile()
                , user.getAvatar(), user.getOpenId());
    }

    @Override
    public int updateLastLoginTime(int id) {
        String sql = "update `t_wx_user` set `last_login_time` = ? where id = ?";
 // 得到 yyyy-MM-dd 格式的当前时间
 String lastLoginTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
 return super.executeUpdate(sql, lastLoginTime , id);
    }

    @Override
    public int updateWxUser(WxUser wxUser) {
        return 0;
    }

    @Override
    public WxUser findById(int userId) {
        return null;
    }
}
